package com.lanyou.cook.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.JsonObject;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.domain.InterfaceSchedule;
import com.lanyou.cook.entity.domain.SystemInterface;
import com.lanyou.cook.repository.InterfaceScheduleRepository;
import com.lanyou.cook.service.KettleJobSender;
import com.lanyou.cook.service.RunningInterfaceWatcher;
import com.lanyou.cook.service.vo.InterfaceExecutionParam;
import com.lanyou.cook.util.gson.GsonUtil;

/**
 * kettle作业调度相关业务控制层
 * @author 仇伟业
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/kettleJob")
public class KettleJobController {
	@Autowired 
	private KettleJobSender kettleJobSender;
	@Autowired 
	private RunningInterfaceWatcher runningInterfaceWatcher;
	@Autowired
	private InterfaceScheduleRepository interfaceScheduleRepository;
	
	/**
	 * 根据接口调度ID执行相对应的作业
	 * @param paramStr 类型:String 作业执行参数
	 * @return 类型:String 成功执行返回true,失败返回false
	 */
	@RequestMapping(value = "/runJobByInterfaceScheduleId", method=RequestMethod.POST, produces="application/json")
	public String runJobByInterfaceScheduleId(@RequestBody String paramStr){
		JsonObject jsonParam = GsonUtil.getDefaultJsonParser().parse(paramStr).getAsJsonObject();
		String interfaceScheduleId = GsonUtil.getString(jsonParam, "interfaceScheduleId", null);
		InterfaceSchedule interfaceSchedule = interfaceScheduleRepository.findOne(InterfaceSchedule.idSpecification(interfaceScheduleId));
		
		SystemInterface systemInterface = interfaceSchedule.getSystemInterface();
		FieldSystem ownerFieldSystem = systemInterface.getOwnerFieldSystem();

		JsonObject result = new JsonObject();
		if(runningInterfaceWatcher.isRunning(systemInterface.getId())) {
			result.addProperty("startSuccess", false);
			result.addProperty("errorCode", "RUNNING");
			return result.toString();
		}
		
		InterfaceExecutionParam param = new InterfaceExecutionParam();
		param.setFieldName(ownerFieldSystem.getField().getName());
		param.setFieldSystemId(ownerFieldSystem.getId());
		param.setFieldSystemName(ownerFieldSystem.getName());
		param.setSystemInterfaceId(systemInterface.getId());
		param.setSystemInterfaceName(systemInterface.getJobName());
		param.setInterfaceScheduleId(interfaceScheduleId);
		param.setInterfaceScheduleName(interfaceSchedule.getName());
		param.setKettleJobId(systemInterface.getKettleJob().getId());
		param.setIsManual(true);
		param.setScheduleTime(new Date());
		param.setOverTimes(interfaceSchedule.getOverTimes());
		
		boolean sended = kettleJobSender.send(param);
		
		result.addProperty("startSuccess", sended);
		return result.toString();
	}
	
}
